09. Exercise: Parsing the JSON Response

L8 13 Exercise- Parsing The JSON Response SC

Now it's your turn to complete this exercise yourself!

In this step you'll make all that JSON text a little more readable. If you want to start at this step, you can download this exercise code from: Step.02-Exercise-Parsing-JSON-With-Moshi. You will find plenty of //TODO comments to help you complete things.

  1. In app/build.gradle, add dependencies for Moshi, Moshi-Kotlin, and the Retrofit Moshi converter, then sync your project.
    implementation "com.squareup.moshi:moshi:$version_moshi"
    implementation "com.squareup.moshi:moshi-kotlin:$version_moshi"

    implementation "com.squareup.retrofit2:retrofit:$version_retrofit"
    implementation "com.squareup.retrofit2:converter-moshi:$version_retrofit"



  1. In MarsProperty.kt, convert the class to a Kotlin data class with properties that match the JSON response fields:
data class MarsProperty(
        val id: String,
        val img_src: String,
        val type: String,
        val price: Double)



  1. Rename the img_src class property to imgSrcUrl, and add a @Json annotation to remap the img_src JSON field to it:
        @Json(name = "img_src") 
        val imgSrcUrl: String,



  1. In MarsApiService.kt, use the Moshi Builder to create a Moshi object with the KotlinJsonAdapterFactory:
 private val moshi = Moshi.Builder()
       .add(KotlinJsonAdapterFactory())
       .build()



  1. In the retrofit object, change the ConverterFactory to a MoshiConverterFactory with our moshi Object:
.addConverterFactory(MoshiConverterFactory.create(moshi))



  1. Update MarsApiService getProperties() method to return a List of MarsProperty objects instead of String:
    @GET("realestate")
    fun getProperties():
            Call<List<MarsProperty>>


  1. In OverviewModel.kt, update getMarsRealEstateProperties() to handle list of MarsProperty instead of String.

    A successful response should include the number of real estate properties returned, response.body().size:

      private fun getMarsRealEstateProperties() {
        MarsApi.retrofitService.getProperties().enqueue( object: Callback<List<MarsProperty>> {
            override fun onFailure(call: Call<List<MarsProperty>>, t: Throwable) {
                _response.value = "Failure: " + t.message
            }

            override fun onResponse(call: Call<List<MarsProperty>>, response: Response<List<MarsProperty>>) {
                _response.value = "Success: ${response.body()?.size} Mars properties retrieved"
            }
        })
    }
      


  1. Build and run the app. You should see a single message showing the number of properties in the response.

If you get stuck, go back and watch the video again. Once you’re done, you can check your solution against the solution we’ve provided here: Step.02-Solution-Parsing-JSON-With-Moshi, or using this git diff.

Task Description:

Complete the tasks below to parse the JSON that's returned from your internet request.

Task List:

Task Feedback:

Great job!